//
//  ReadLoginData.m
//  EasyToTesco
//
//  Created by mc on 16/3/14.
//  Copyright © 2016年 Jack. All rights reserved.
//

#import "ReadLoginData.h"
#import "SellerUserInfoModel.h"

@interface ReadLoginData ()

//@property (nonatomic,copy)NSString *userName;

//{
//    NSString *_userName;
//}

@end

@implementation ReadLoginData

#pragma mark 环信
+ (void)logoutAction:(UIView *)ShowView {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        EMError *error = [[EMClient sharedClient] logout:YES];
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:ShowView animated:YES];
            if (error != nil) {
                [MBProgressHUD show:ShowView andMessage:[NSString stringWithFormat:@"%@",error.errorDescription]];
                MBProgressHUD *hud = [MBProgressHUD showMessage:[NSString stringWithFormat:@"%@",error.errorDescription] toView:ShowView dimBackground:YES AndCompleteBlock:nil];
                [ShowView addSubview:hud];
            }
            else{
                NSLog(@"注销成功");
                [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
            }
        });
    });
}


+ (void)removeDataToLocal:(NSString *)plistPath {
    NSString *fileName = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:plistPath];
    
    NSFileManager *fm = [NSFileManager defaultManager];
    
    if ([fm fileExistsAtPath:fileName]) {
        
        NSError *error = nil;
        [fm removeItemAtPath:fileName error:&error];
    }
    
}

+(void)saveAddressDataToLocal:(NSDictionary *)userAddressDict {
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths objectAtIndex:0];
    NSLog(@"path = %@",path);
    NSString *filename=[path stringByAppendingPathComponent:@"addressData.plist"];
    //创建
    NSFileManager* fm = [NSFileManager defaultManager];
    [fm createFileAtPath:filename contents:nil attributes:nil];
    
    [userAddressDict writeToFile:filename atomically:YES];
    
}

+ (void)cleanloginData {
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths objectAtIndex:0];
    //    NSLog(@"path = %@",path);
    NSString *filename=[path stringByAppendingPathComponent:@"loginData.plist"];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dic writeToFile:filename atomically:YES];
    
}

+ (NSMutableDictionary *)readAddressData {
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths objectAtIndex:0];
    
    //    NSLog(@"---%@", path);
    //获取一个plist文件
    NSString *filename=[path stringByAppendingPathComponent:@"addressData.plist"];
    //读文件
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:filename];//[NSMutableArray arrayWithContentsOfFile:filename];
    return dic;
}

+ (void)saveShopDataToLocal:(NSMutableArray *)shopDataArr {
    
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];
    NSLog(@"%@", shopDataArr);
    NSLog(@"path = %@",path);
    NSString *filename=[path stringByAppendingPathComponent:@"shopData.plist"];
    //创建
    NSFileManager* fm = [NSFileManager defaultManager];
    [fm createFileAtPath:filename contents:nil attributes:nil];
    BOOL a = [shopDataArr writeToFile:filename atomically:YES];
    
    if (a) {
        NSLog(@"写入成功");
    } else {
        NSLog(@"写入失败");
    }
    
}

+ (NSMutableArray *)readShopArr {
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];
    
    //    NSLog(@"---%@", path);
    //获取一个plist文件
    NSString *filename=[path stringByAppendingPathComponent:@"shopData.plist"];
    //读文件
    NSMutableArray *readShopArr = [NSMutableArray arrayWithContentsOfFile:filename];//[NSMutableArray arrayWithContentsOfFile:filename];
    return readShopArr;
}

+ (void)savelogindataToLocal:(NSDictionary *)userDataDict{
    
    
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths objectAtIndex:0];
    NSLog(@"path = %@",path);
    NSString *filename=[path stringByAppendingPathComponent:@"loginData.plist"];
    //创建
    NSFileManager* fm = [NSFileManager defaultManager];
//    [fm createFileAtPath:filename contents:nil attributes:nil];
    
    BOOL judge = [fm createFileAtPath:filename contents:nil attributes:nil];
    
    NSLog(@"%d",(int)judge);
    
    [userDataDict writeToFile:filename atomically:YES];
    
}

+ (void)savelogindataToLocal:(NSDictionary *)userDataDict AndPlistPath:(NSString *) string {
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths objectAtIndex:0];
    NSLog(@"path = %@",path);
    NSString *filename=[path stringByAppendingPathComponent:string];
    //创建
    NSFileManager* fm = [NSFileManager defaultManager];
    [fm createFileAtPath:filename contents:nil attributes:nil];
    
    [userDataDict writeToFile:filename atomically:YES];
}

+ (NSMutableDictionary *)readLoginData {
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths objectAtIndex:0];
    
    //    NSLog(@"---%@", path);
    //获取一个plist文件
    NSString *filename=[path stringByAppendingPathComponent:@"loginData.plist"];
    //读文件
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:filename];//[NSMutableArray arrayWithContentsOfFile:filename];
    return dic;
}

+ (NSMutableDictionary *)readLoginDataWithPathPlistString:(NSString *)plistPath {
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths objectAtIndex:0];
    
    //    NSLog(@"---%@", path);
    //获取一个plist文件
    NSString *filename=[path stringByAppendingPathComponent:plistPath];
    //读文件
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:filename];//[NSMutableArray arrayWithContentsOfFile:filename];
    return dic;
}

+ (void)reloadUserInfoWithUserNumber:(NSString *)userNumber andInfoModel:(reloadUserInfo)userInfo {
    
//    NSLog(@"%@",userNumber);
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSDictionary *params = @{
                                 @"username":userNumber
                                 };
        
        [[NetWorkSharedTools sharedNetWorkTools] GetObjectWithURLDict:params AndMenthodName:@"/api/Chat/memberInfo" completeBlock:^(id object) {
//            NSLog(@"%@",object);
            if (JudgeBackDataCode) {
                NSDictionary *dict = [object objectForKey:@"data"];
                SellerUserInfoModel *infoModel = [[SellerUserInfoModel alloc] initWithDictionary:dict error:nil];
                dispatch_async(dispatch_get_main_queue(), ^{
                    userInfo(infoModel);
                });
            }
            
        }];
        
    });
}

+ (void)loadLocationImageView:(NSString *)urlStirng andModel:(id<IConversationModel>)model andImageView:(imageView)returnImage{
    
    //    NSArray *strArr = [urlStirng componentsSeparatedByString:@"/"];
    //
    //    NSString *pKey = strArr[strArr.count - 1];
    NSString *pKey = [NSString stringWithFormat:@"%@.jpg",model.title];
    NSLog(@"%@",pKey);
    
    if ([[SDImageCache sharedImageCache] imageFromDiskCacheForKey:pKey]) {
        NSLog(@"缓存");
        returnImage([[SDImageCache sharedImageCache] imageFromDiskCacheForKey:pKey]);
        
    } else {
        
        SDWebImageManager *manager = [SDWebImageManager sharedManager];
        [manager downloadImageWithURL:[NSURL URLWithString:urlStirng] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            if (image) {
                NSLog(@"存储");
                [[SDImageCache sharedImageCache] storeImage:image
                 
                                                     forKey:pKey
                 
                                                     toDisk:YES];
                
                returnImage(image);
                
                // do something with image
                
            } else {
                returnImage(model.avatarImage);
            }
        }];
    }
}


@end
